package com.example.administrator.myapplication;


import android.content.Context;
import android.os.Bundle;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.myapplication.model.User;

import java.util.Timer;
import java.util.TimerTask;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MeFragment extends MyBaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM_USER_OBJECT = "user_object";
    private static final String ARG_PARAM2 = "param2";
    private static final String TAG = "MeFragment";

    private Fragment fragment;
    private RelativeLayout we_chat_relative;
    private TextView me_nickname_tv; //用户昵称
    private TextView me_wechat_account;
    private ImageView me_QR_icon;
    private RelativeLayout settings_relative;
   private FragmentManager fragmentManager;
    private User user;
    // TODO: Rename and change types of parameters



    public MeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MeFragment newInstance(String param1, String param2) {
        MeFragment fragment = new MeFragment();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    public static MeFragment newInstance(User param1_account) {
        Log.i(TAG, "newInstance: 传入的参数为 :"+param1_account.getUsername());
        MeFragment fragment = new MeFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM_USER_OBJECT, param1_account);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        fragmentManager = getActivity().getSupportFragmentManager();

        fragment = fragmentManager.findFragmentById(R.id.content);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            user = ((User) getArguments().getSerializable(ARG_PARAM_USER_OBJECT));
        }else {
            user = new User();
        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_me, container, false);
        we_chat_relative = ((RelativeLayout) view.findViewById(R.id.we_chat_relative));
        me_QR_icon = ((ImageView) view.findViewById(R.id.me_QR_icon));
        settings_relative = ((RelativeLayout) view.findViewById(R.id.settings_relative));
        me_nickname_tv = ((TextView) view.findViewById(R.id.me_nickname));
       me_wechat_account = ((TextView) view.findViewById(R.id.me_wechat_account));
        me_nickname_tv.setText(user.getUsername());
        me_wechat_account.setText(me_wechat_account.getText()+user.getUsername());

        we_chat_relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG,"查看我的微信账户！");
            }
        });
        me_QR_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG,"查看我的二维码");
            }
        });
        settings_relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG,"进入设置界面");
                fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
                    @Override
                    public void onBackStackChanged() {
                        Log.i(TAG,"后退栈发生变化！");
                    }
                });
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        fragment = new SettingsFragment();
                        //默认将当期Fragment压如栈中，当触发Back按钮时自动将上面一个销毁并重新显示当前Fragment，从而实现后退的效果
                        fragmentManager.beginTransaction().replace(R.id.content, fragment).addToBackStack(null).commit();
                    }
                },500);




            }
        });




        // Inflate the layout for this fragment
        return view;
    }


    @Override
    public void onStart() {
        super.onStart();
    }
    @Override
    public void onResume() {
        super.onResume();
        getActivity().setTitle(R.string.title_me);
    }
}
